﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FatoryTool
{
    public class CalcTools
    {
        public static System.Windows.Point GetCenterPoint(System.Windows.Point P1, System.Windows.Point P2)
        {
            return new System.Windows.Point(
                Math.Min(P2.X, P1.X) + (Math.Max(P2.X, P1.X) - Math.Min(P2.X, P1.X)) / 2
                , Math.Min(P2.Y, P1.Y) + (Math.Max(P2.Y, P1.Y) - Math.Min(P2.Y, P1.Y)) / 2);
        }
        public static System.Windows.Point GetCenterPoint(System.Drawing.PointF P1, System.Drawing.PointF P2)
        {
            return new System.Windows.Point(
                Math.Min(P2.X, P1.X) + (Math.Max(P2.X, P1.X) - Math.Min(P2.X, P1.X)) / 2
                , Math.Min(P2.Y, P1.Y) + (Math.Max(P2.Y, P1.Y) - Math.Min(P2.Y, P1.Y)) / 2);
        }
        public static System.Drawing.PointF WinPoint2DrawPointF(System.Windows.Point point)
        {
            return new System.Drawing.PointF((float)point.X, (float)point.Y);
        }
        public static System.Windows.Point WinPoint2DrawPointF(System.Drawing.PointF point)
        {
            return new System.Windows.Point(point.X, point.Y);
        }
        public static System.Windows.Point GetIntersection5_175(
                    System.Drawing.PointF lineFirstStart, System.Drawing.PointF lineFirstEnd,
                    System.Drawing.PointF lineSecondStart, System.Drawing.PointF lineSecondEnd) //交点
        {
            //x0 = ((x3 - x4) * (x2 * y1 - x1 * y2) - (x1 - x2) * (x4 * y3 - x3 * y4)) / ((x3 - x4) * (y1 - y2) - (x1 - x2) * (y3 - y4));
            //y0 = ((y3 - y4) * (y2 * x1 - y1 * x2) - (y1 - y2) * (y4 * x3 - y3 * x4)) / ((y3 - y4) * (x1 - x2) - (y1 - y2) * (x3 - x4));
            System.Windows.Point cpoint = new System.Windows.Point();
            double x = ((lineSecondStart.X - lineSecondEnd.X) * (lineFirstEnd.X * lineFirstStart.Y - lineFirstStart.X * lineFirstEnd.Y)
                - (lineFirstStart.X - lineFirstEnd.X) * (lineSecondEnd.X * lineSecondStart.Y - lineSecondStart.X * lineSecondEnd.Y))
                / ((lineSecondStart.X - lineSecondEnd.X) * (lineFirstStart.Y - lineFirstEnd.Y) - (lineFirstStart.X - lineFirstEnd.X) * (lineSecondStart.Y - lineSecondEnd.Y));
            cpoint.X = x;
            double y = ((lineSecondStart.Y - lineSecondEnd.Y) * (lineFirstEnd.Y * lineFirstStart.X - lineFirstStart.Y * lineFirstEnd.X)
                - (lineFirstStart.Y - lineFirstEnd.Y) * (lineSecondEnd.Y * lineSecondStart.X - lineSecondStart.Y * lineSecondEnd.X))
                / ((lineSecondStart.Y - lineSecondEnd.Y) * (lineFirstStart.X - lineFirstEnd.X) - (lineFirstStart.Y - lineFirstEnd.Y) * (lineSecondStart.X - lineSecondEnd.X));
            cpoint.Y = y;
            return cpoint;
        }
        public static System.Drawing.PointF LinePointProjection(System.Drawing.PointF P1, System.Drawing.PointF P2, System.Drawing.PointF P3)
        {
            double a1 = P2.X - P1.X;
            double b1 = P2.Y - P1.Y;
            double y1 = P1.Y;
            double x1 = P1.X;
            double y2 = P2.Y;
            double x2 = P2.X;
            double y3 = P3.Y;
            double x3 = P3.X;
            double a1a1 = Math.Pow(a1, 2.0);
            double b1b1 = Math.Pow(b1, 2.0);
            double denominator = a1a1 + b1b1;
            if (denominator == 0) return P3;

            double x1y2 = x1 * y2;
            double x2y1 = x2 * y1;
            double a1b1 = a1 * b1;
            double moleculey = b1b1 * y3 + a1b1 * x3 - a1 * x1y2 + a1 * x2y1;
            double moleculex = a1a1 * x3 + a1b1 * y3 - b1 * x2y1 + b1 * x1y2;

            return new System.Drawing.PointF((float)(moleculex / denominator), (float)(moleculey / denominator));
        }
        public static System.Windows.Point LinePointProjection(System.Drawing.PointF P1, System.Drawing.PointF P2, System.Windows.Point P3)
        {
            double a1 = P2.X - P1.X;
            double b1 = P2.Y - P1.Y;
            double y1 = P1.Y;
            double x1 = P1.X;
            double y2 = P2.Y;
            double x2 = P2.X;
            double y3 = P3.Y;
            double x3 = P3.X;
            double a1a1 = Math.Pow(a1, 2.0);
            double b1b1 = Math.Pow(b1, 2.0);
            double denominator = a1a1 + b1b1;
            if (denominator == 0) return P3;

            double x1y2 = x1 * y2;
            double x2y1 = x2 * y1;
            double a1b1 = a1 * b1;
            double moleculey = b1b1 * y3 + a1b1 * x3 - a1 * x1y2 + a1 * x2y1;
            double moleculex = a1a1 * x3 + a1b1 * y3 - b1 * x2y1 + b1 * x1y2;

            return new System.Windows.Point(moleculex / denominator, moleculey / denominator);
        }

        public static double Angle(System.Windows.Point cen, System.Windows.Point first, System.Windows.Point second)
        {
            const double M_PI = 3.1415926535897;

            double ma_x = first.X - cen.X;
            double ma_y = first.Y - cen.Y;
            double mb_x = second.X - cen.X;
            double mb_y = second.Y - cen.Y;
            double v1 = (ma_x * mb_x) + (ma_y * mb_y);
            double ma_val = Math.Sqrt(ma_x * ma_x + ma_y * ma_y);
            double mb_val = Math.Sqrt(mb_x * mb_x + mb_y * mb_y);
            double cosM = v1 / (ma_val * mb_val);
            double angleAMB = Math.Acos(cosM) * 180 / M_PI;

            return angleAMB;
        }
        public static double Distance(double x1,double y1,double x2,double y2)
        {
            double xdiff = x1 - x2;
            double ydiff = y1 - y2;

            return Math.Sqrt(xdiff * xdiff + ydiff * ydiff);
        }

        public enum VectorClockDirection
        {
            None,
            Clockwise,
            CounterClockwise
        }
        public static VectorClockDirection VectorClockDirectionCalc(System.Windows.Point P1, System.Windows.Point P2, System.Windows.Point P3)
        {
            double p12p23 = (P2.X - P1.X) * (P3.Y - P2.Y) - (P2.Y - P1.Y) * (P3.X - P2.X);
            if (p12p23 > 0) { return VectorClockDirection.CounterClockwise; }
            else if (p12p23 < 0) { return VectorClockDirection.Clockwise; }
            else { return VectorClockDirection.None; }
        }
       
    }
}

